﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Text;
using System.Linq;

/*
 *	
 *  Base View
 *
 *	by Xuanyi
 *
 */

namespace UIFramework
{
	public abstract class BaseView : MonoBehaviour
    {
        public string ClassName;               

        public void Awake()
        {
            ClassName = this.gameObject.name;
            //Debug.Log("awake : " + ClassName); 
        }

        public virtual void OnEnter(BaseContext context)
        {            
        }

        public virtual void OnExit(BaseContext context)
        {
            Singleton<UIManager>.Instance.DestroySingleUI(context.ViewType);
        }

        public virtual void OnPause(BaseContext context)
        {
        }

        public virtual void OnResume(BaseContext context)
        {
        }

        public void DestroySelf()
        {
            Destroy(gameObject);
        }

        public GameObject FindObject(string path)
        {
            GameObject go = this.gameObject.transform.Find(path).gameObject;
            if(go == null)
            {
                Debug.LogError("Path is error::" + path);
                return null;
            }
            return go;
        }

        public GameObject FindObject(string path,GameObject parent)
        {
            GameObject go = parent.transform.Find(path).gameObject;
            if (go == null)
            {
                Debug.LogError("Path is error::" + path);
                return null;
            }
            return go;
        }

        public T FindComponent<T>(string path) where T:Component
        {
            GameObject go = this.gameObject.transform.Find(path).gameObject;
            if(go == null)
            {
                Debug.LogError("Path is error::" + path);                
            }

            T c  = go.GetComponent<T>();

            if(c == null)
            {
                Debug.LogError("Component is null");
            }

            return c;
        }

        public T FindComponent<T>(string path,GameObject parent) where T : Component
        {
            GameObject go = parent.transform.Find(path).gameObject;
            if (go == null)
            {
                Debug.LogError("Path is error::" + path);
            }

            T c = go.GetComponent<T>();

            if (c == null)
            {
                Debug.LogError("Component is null");
            }

            return c;
        }

        public List<int> GetIntArray(string item)
        {
            var props = new List<int>();

            if (item == null)
            {
                return props;
            }

            foreach (string s in item.Split("-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                int p;
                if (int.TryParse(s, out p))
                {
                    props.Add(p);
                }
            }

            return props;
        }

        public List<string> GetStringArray(string item)
        {
            if (string.IsNullOrEmpty(item))
            {
                return new List<string>();
            }

            return new List<string>(item.Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
        }        

        public void Update()
        {
            AppUpdate();
        }

        public void DestoryChild(GameObject go)
        {
            for (int i = 0; i < go.transform.childCount; i++)
            {
                go.transform.GetChild(i).gameObject.SetActive(false);
                GameObject.Destroy(go.transform.GetChild(i).gameObject);
            }
        }

        public void AddChild(GameObject item, GameObject parent)
        {
            item.transform.SetParent(parent.transform);            
            item.transform.localScale = Vector3.one;
            item.transform.localPosition = Vector3.zero;
            item.SetActive(true);
        }

        public virtual void HandleAndroidEscape()
        {

        }

        public virtual void AppUpdate()
        { 
        
        }
        

	}
}
